# -*- coding: utf-8 -*-
# @Time    : 2018/8/15 13:27
# @Author  : 聂家利
# @Email   : 1073438012@qq.com
# @File    : centerUi.py
# @Software: PyCharm
from .import *
from its_auth.common import is_d_or_other
from its_auth.lib import Message
from its_auth.build_ui.api.centeruiapi import CenterUiApi


class center_ui(QtWidgets.QDialog):
    filename = ''
    now_scene_dir = ''

    def __init__(self):
        super(center_ui, self).__init__()
        self.ui_message = Message()
        self.init_ui()

    def init_ui(self):
        self.share_dir_edit = QtWidgets.QLineEdit(r'C:\Users\ITS-njl\Desktop\test')
        self.share_dir_edit.setPlaceholderText(u'共享文件夹目录')

        self.file_save_edit = QtWidgets.QLineEdit(r'C:\Users\ITS-njl\Desktop\dec\test_o.ma')
        self.file_save_edit.setPlaceholderText(u'文件保存到服务器地址，从cgtw获取')

        self.scene_edit = QtWidgets.QLineEdit(os.path.basename(pm.sceneName()))
        self.submit_server_btn = QtWidgets.QPushButton(u'提交此文件到服务器', self, clicked=self.submit_file_to_server)

        grid = QtWidgets.QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(self.share_dir_edit, 1, 0)
        grid.addWidget(self.file_save_edit, 2, 0)
        grid.addWidget(self.scene_edit, 3, 0)
        grid.addWidget(self.submit_server_btn , 3, 1)

        # grid.addWidget(self.suffix_lable, 2, 1)
        self.setLayout(grid)
        self.setWindowTitle('grid layout')
        self.resize(350, 300)

    def open_file(self):
        self.now_scene_dir = os.path.dirname(pm.sceneName())
        self.filename = QtWidgets.QFileDialog.getExistingDirectory(self, 'Open file', self.now_scene_dir,
                                                              QtWidgets.QFileDialog.DontUseNativeDialog)

    def submit_file_to_server(self):
        file_save_path, net_share_path = self.get_path()
        # 发送requests 请求 [发送内容：user_name, host_ip , 文件全路径，文件保存路径]
        Con = CenterUiApi(user_name='njl',  # 服务器记录日志专用
                          file_path=net_share_path,
                          file_save_path=file_save_path  # cgtw 获取
         )
        # 发送数据到服务器
        r = Con.send_data()
        print('url', r.url)
        print('server return data', r.text)

    def get_path(self):
        # todo 如果输入的目录和文件必须一一对应
        file_name = self.scene_edit.text().strip()
        share_dir = self.share_dir_edit.text().strip()
        file_save_path = self.file_save_edit.text().strip()
        if file_name:
            net_share_path = (share_dir+'/'+file_name).replace('/', '\\')
        else:
            net_share_path = share_dir
        return r'{}'.format(file_save_path), r'{}'.format(net_share_path)


def main():
    global center_ui
    center_ui = center_ui()
    center_ui.show()

if __name__ == '__main__':
    main()